package pt.uminho.pimj.core.configuration;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class ConfigurationManager {
	private static Logger log = Logger.getLogger(ConfigurationManager.class);
	private static Properties properties = null;
	
	/**
	 * Initializes the configuration system
	 * @throws IOException 
	 */
	public static void initialize () throws IOException { ConfigurationManager.getBundle(); }
	
	private static void getBundle () throws IOException {
		if (properties == null) {
			properties = new Properties();
			properties.load(ConfigurationManager.getConfigurationFile().openStream());
			PropertyConfigurator.configure(ConfigurationManager.getConfigurationFile());
			ConfigurationManager.log.trace("Log4j Configured");
			ConfigurationManager.log.trace("Configuration File loaded");
		}
	}
	public static URL getConfigurationFile () {
		return ConfigurationManager.class.getResource("PiMj.config");
	}
	public static String getProperty (String key, String defaultValue) {
		try {
			ConfigurationManager.getBundle();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String res = properties.getProperty(key, defaultValue);
		ConfigurationManager.log.trace("Property '"+key+"' = "+res);
		if (res == null) return defaultValue;
		else return res.trim();
	}
	public static List<String> getProperties (String key, List<String> defaultValue) {
		try {
			ConfigurationManager.getBundle();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String res = properties.getProperty(key, null);
		ConfigurationManager.log.trace("Property '"+key+"' = "+res);
		if (res == null) return defaultValue;
		String[] list = res.split(",");
		ArrayList<String> result = new ArrayList<String>();
		for (String p : list) result.add(p.trim());
		return result;
	}
	public static List<String> getProperties (String key, String[] defaultValue) {
		try {
			ConfigurationManager.getBundle();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String res = properties.getProperty(key, null);
		ConfigurationManager.log.trace("Property '"+key+"' = "+res);
		if (res == null) return Arrays.asList(defaultValue);
		String[] list = res.split(",");
		ArrayList<String> result = new ArrayList<String>();
		for (String p : list) result.add(p.trim());
		return result;
	}
	public static boolean getProperty (String key, boolean defaultValue) {
		try {
			ConfigurationManager.getBundle();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String res = properties.getProperty(key,null);
		ConfigurationManager.log.trace("Property '"+key+"' = "+res);
		if (res == null) return defaultValue;
		else {
			res = res.trim().toLowerCase();
			if (res.matches("1|0|y|n|yes|no|true|false")) {
				if (res.equals("0")) return false;
				else if (res.equals("1")) return true;
				else if (res.equals("no")) return false;
				else if (res.equals("yes")) return true;
				else if (res.equals("n")) return false;
				else if (res.equals("y")) return true;
				else if (res.equals("false")) return false;
				else if (res.equals("true")) return true;
				else return defaultValue;
			} else return defaultValue;
		}
	}
	public static int getProperty (String key, int defaultValue) {
		try {
			ConfigurationManager.getBundle();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String res = properties.getProperty(key, null);
		ConfigurationManager.log.trace("Property '"+key+"' = "+res);
		if (res == null) return defaultValue;
		else {
			if (res.trim().matches("[0-9]+")) return Integer.parseInt(res.trim());
			else return defaultValue;
		}
	}
	public static float getProperty (String key, float defaultValue) {
		try {
			ConfigurationManager.getBundle();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String res = properties.getProperty(key, null);
		ConfigurationManager.log.trace("Property '"+key+"' = "+res);
		if (res == null) return defaultValue;
		else {
			return Float.parseFloat(res.trim());
		}
	}
}
